package com.kelvin.paopao.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.easeui.utils.EaseCommonUtils;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.kelvin.kelvinlib.db.dao.DaoSession;
import com.kelvin.kelvinlib.db.dao.UserInfoDao;
import com.kelvin.kelvinlib.imageloader.GlideUtil;
import com.kelvin.kelvinlib.utils.CommonUtil;
import com.kelvin.paopao.R;

import java.util.Date;


/**
 * Created by MCK on 2017/5/9.
 */
public class MessageAdapter extends BaseQuickAdapter<EMConversation, BaseViewHolder> {

    private Context context;
    private UserInfoDao dao;

    public MessageAdapter(Context context, DaoSession daoSession) {
        super(R.layout.recycler_message_item);
        this.context = context;
        dao = daoSession.getUserInfoDao();
    }

    @Override
    protected void convert(BaseViewHolder helper, EMConversation item) {
        TextView tvName = helper.getView(R.id.tv_name);
        TextView tv_detail = helper.getView(R.id.tv_detail);
        tv_detail.setVisibility(View.INVISIBLE);

        tvName.setText(EaseSmileUtils.getSmiledText(context, EaseCommonUtils.
                getMessageDigest(item.getLastMessage()
                        , (this.context))), TextView.BufferType.SPANNABLE);
        ImageView iv = helper.getView(R.id.iv_touxiang);
        String user_headimg = dao.load(Long.parseLong(
                item.conversationId()))
                .getAvatar();
        GlideUtil.loadImgCircle(user_headimg, iv, context, R.drawable.default_img);
        Date lastMsgDate = new Date(item.getLastMessage().getMsgTime());
        String lastMsgTime = CommonUtil.parseDateTime(lastMsgDate, "MM月dd日");
        helper.setText(R.id.tv_date, lastMsgTime);

        TextView tvUnreadCount = helper.getView(R.id.unread_msg_number);
        if (item.getUnreadMsgCount() != 0) {
            tvUnreadCount.setVisibility(View.VISIBLE);
            tvUnreadCount.setText(item.getUnreadMsgCount() + "");
        } else {
            tvUnreadCount.setVisibility(View.GONE);
        }
        RelativeLayout rlDelelte = helper.getView(R.id.rl_delete);
        if (showDelete) {
            rlDelelte.setVisibility(View.VISIBLE);
        } else {
            rlDelelte.setVisibility(View.GONE);
        }
        helper.addOnClickListener(R.id.rl_delete);
    }

    private boolean showDelete;

    public void setDelete(boolean showDelete) {
        this.showDelete = showDelete;
        notifyDataSetChanged();
    }
}
